﻿Imports System.Data.SqlClient
Imports UTNComputers.clsRoles
Public Class daoRoles
    'Funcion para Traer un Rol por Id'
    Public Function TraeRol(ByVal rol_id As Integer) As clsRoles
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim Rol As New clsRoles()

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_ROL"

            Cm.Parameters.Add("@rol_id", SqlDbType.Int)
            Cm.Parameters("@rol_id").Value = rol_id

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Rol.rol_id = Dr.GetInt32(0)
                    Rol.Descri = Dr.GetString(1).Trim.ToUpper
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return Rol
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    'Funcion para Traer Todos los Roles'
    Public Function TraeRoles() As List(Of clsRoles)
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim ListaRoles As New List(Of clsRoles)

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_ROLES_TODOS"

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Dim Rol As New clsRoles()
                    Rol.rol_id = Dr.GetInt32(0)
                    Rol.Descri = Dr.GetString(1).Trim.ToUpper
                    ListaRoles.Add(Rol)
                    Rol.Dispose()
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return ListaRoles
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    '---------------------------------------------------------------------
    ' Constructor de la Clase
    '---------------------------------------------------------------------
    Public Sub New()

        MyBase.New()

    End Sub

    '---------------------------------------------------------------------
    ' Destructor de la Clase
    '---------------------------------------------------------------------
    Public Sub Dispose()

        MyBase.Finalize()

    End Sub

End Class
